package com.design.android.personnel;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.design.android.personnel.bean.PersonnelInfo;
import com.design.android.personnel.bean.User;
import com.design.android.personnel.database.PersonnelInfoOperate;
import com.design.android.personnel.database.UserOperate;
import com.design.android.personnel.ui.LoginActivity;
import com.design.android.personnel.utils.DateUtils;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.io.File;
import java.io.IOException;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    private UserOperate userOperate;
    private PersonnelInfoOperate personnelInfoOperate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView navView = findViewById(R.id.nav_view);

        // 打开 app 进入首页，首页创建判断是否已经登录
        // 未登录重定向到登陆页面
        if (AppContext.getLoggedInUser() == null) {
            redirect2Login();
        }

        // 初始化权限
        requestPermissions();

        // 测试用,初始化数据库
        mockSqlData();

        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_add, R.id.navigation_dashboard, R.id.navigation_other)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);

        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
    }

    private void redirect2Login() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    public void requestPermissions() {
        String[] permissions = {
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS
        };
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(permissions, 0x001);
        }
    }

    private void mockSqlData() {
        File initLock = new File(getExternalCacheDir(), "init.lock");
        if (initLock.exists()) {
            System.out.println("initLock");
            return;
        } else {
            try {
                initLock.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        Date createTime = DateUtils.str2Date("2020-08-10 13:15:36");

        userOperate = new UserOperate(this);

        User user = new User();
        user.setId(1);
        user.setPhone("13344445555");
        user.setPassword("999ac8b33a58dd11b9188cea0a27472e");
        user.setSalt("f883e6");
        user.setUsername("小黑");
        user.setSex("男");
        user.setCreateTime(createTime);
        user.setUpdateTime(createTime);
        user.setRemark("");

        // 添加一条用户记录
        userOperate.add(user);

        personnelInfoOperate = new PersonnelInfoOperate(this);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.avatar);

        for (int i = 0; i < 10; i++) {
            PersonnelInfo personnelInfo = new PersonnelInfo();
            personnelInfo.setId(i + 1);
            personnelInfo.setUserId(1);
            personnelInfo.setAvatar(bitmap);
            personnelInfo.setPhone("1334455667" + 0);
            personnelInfo.setName("大黑" + i);
            personnelInfo.setSex(i % 2 == 0 ? "男" : "女");
            personnelInfo.setAddr("安徽省阜阳市颍上县前进路");
            personnelInfo.setCreateTime(createTime);
            personnelInfo.setUpdateTime(createTime);

            personnelInfoOperate.add(personnelInfo);
        }
    }

    // 申请权限的结果回调
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
